#!/bin/bash
#
# https://github.com/Aniverse/inexistence
# Author: Aniverse

script_update=2020.03.02
script_version=r10005

################################################################################################ Debug

usage_for_debug() {
    export local_packages=/etc/inexistence/00.Installation
    export s=$local_packages/package/rclone/uninstall
    rm -f $s ; nano $s ; bash $s
}

################################################################################################ Get options

unset LogRootPath LogTimes DebLocation SCLocation LOCKLocation OutputLOG

AppName=rclone
AppNameLower=rclone
Need_SourceCode=no
pm_action=uninstall

function show_usage() { echo "
$AppName $pm_action $script_version ($script_update)
Usage:
      -l        Specifiy LogBase
      -d        Enable debug mode
      --log     Show logfile's content"
exit 1 ; }

OPTS=$(getopt -o v:l:d -al version:,debug,logbase:,log -- "$@")
[ ! $? = 0 ] && show_usage
eval set -- "$OPTS"

while [ -n "$1" ] ; do case "$1" in
    -l | --logbase      ) LogTimes="$2" ; shift 2 ;;
    -d | --debug        ) debug=1       ; shift   ;;
         --log          ) show_log=1    ; shift   ;;
    -- ) shift ; break ;;
esac ; done

################################################################################################ Set Variables

if [[ -f /etc/inexistence/00.Installation/function ]]; then
    source /etc/inexistence/00.Installation/function
else
    source <(wget -qO- https://github.com/Aniverse/inexistence/raw/master/00.Installation/function)
fi

set_variables_log_location
check_var_OutputLOG
debug_log_location
cat_outputlog

################################################################################################





function uninstall_rclone() {
    echo >> $OutputLOG

    rm -f /usr/bin/rclone /usr/local/share/man/man1/rclone.1 $LOCKLocation/rclone.lock
    mandb >> $OutputLOG 2>&1

    status_lock=${AppNameLower}-r
    echo "status_lock=$status_lock" > /tmp/Variables
    rm -f /tmp/$status_lock.1.lock /tmp/$status_lock.2.lock 
    { which rclone > /dev/null || touch /tmp/$status_lock.1.lock ; } && touch /tmp/$status_lock.2.lock
    [[ -f /tmp/$status_lock.1.lock ]] && rm -rf $LOCKLocation/$AppNameLower.lock

    echo >> $OutputLOG
}





echo_task "Uninstalling $AppName ..."
uninstall_rclone & spinner $!
check_status $status_lock
